/*
 * GameEngine.cpp
 *
 *  Created on: Aug 13, 2011
 *      Author: Squall_Chua
 */

#include "GameEngine.h"
#include "GameConfig.h"

#define GAME_EXIT	true
#define GAME_CONT	false

namespace projectcos
{
namespace core
{

GameEngine::GameEngine()
: m_HGE(NULL)
, m_TileMap(NULL)
{

}

bool GameEngine::FrameFunc()
{
	if(NULL != GAMEENGINE.m_HGE)
	{
		return GAMEENGINE.Update(GAMEENGINE.m_HGE->Timer_GetDelta());
	}

	return GAME_EXIT;
}

bool GameEngine::RenderFunc()
{
	if(NULL != GAMEENGINE.m_HGE)
	{
		return GAMEENGINE.Render();
	}

	return GAME_EXIT;
}

bool GameEngine::Initialize()
{
	m_HGE = hgeCreate(HGE_VERSION);

	if(NULL != m_HGE)
	{
		m_HGE->System_SetState(HGE_LOGFILE, "hge.log");

		// Set our frame function
		m_HGE->System_SetState(HGE_FRAMEFUNC, GameEngine::FrameFunc);

		// Set our render function
		m_HGE->System_SetState(HGE_RENDERFUNC, GameEngine::RenderFunc);

		// Set the window title
		m_HGE->System_SetState(HGE_TITLE, GAME_TITLE);

		// Set windowed mode
		m_HGE->System_SetState(HGE_WINDOWED, WINDOWED_MODE);

		// Set the window width and height
		m_HGE->System_SetState(HGE_SCREENWIDTH, WINDOW_WIDTH);
		m_HGE->System_SetState(HGE_SCREENHEIGHT, WINDOW_HEIGHT);

		// Set the window BPP
		m_HGE->System_SetState(HGE_SCREENBPP, WINDOW_BPP);

		// Use BASS for sound
		m_HGE->System_SetState(HGE_USESOUND, USE_BASS_SOUND);

//		m_HGE->System_SetState(HGE_FPS, 100);


		// Initialize system
		if(m_HGE->System_Initiate() == false)
		{
			// If HGE initialization failed show error message
			MessageBox(NULL, m_HGE->System_GetErrorMessage(), "Error", MB_OK | MB_ICONERROR | MB_APPLMODAL);
			return false;
		}
		else
		{


//			quad.blend = BLEND_DEFAULT;
//
//			for(int i=0;i<4;i++)
//			{
//				// Set up z-coordinate of vertices
//				quad.v[i].z=0.5f;
//				// Set up color. The format of DWORD col is 0xAARRGGBB
//				quad.v[i].col=0xFFFFFFFF;
//			}
//
//			quad.tex= m_HGE->Texture_Load("../asset/tmw_desert_spacing.png");
//			if(!quad.tex)
//			{
//				return false;
//			}
//
//			float actWidth = m_HGE->Texture_GetWidth(quad.tex, true);
//			float actHeight = m_HGE->Texture_GetHeight(quad.tex, true);
//			float width = m_HGE->Texture_GetWidth(quad.tex);
//			float height = m_HGE->Texture_GetHeight(quad.tex);
//
//			quad.v[0].x=0; quad.v[0].y=0;
//			quad.v[1].x=4*actWidth/8; quad.v[1].y=0;
//			quad.v[2].x=4*actWidth/8; quad.v[2].y=4*actHeight/6;
//			quad.v[3].x=0; quad.v[3].y=4*actHeight/6;
//
//			quad.v[0].tx=0; quad.v[0].ty=0;
//			quad.v[1].tx=(4*actWidth/8)/width; quad.v[1].ty=0;
//			quad.v[2].tx=(4*actWidth/8)/width; quad.v[2].ty=(4*actHeight/6)/height;
//			quad.v[3].tx=0; quad.v[3].ty=(4*actHeight/6)/height;

			m_TileMap = new projectcos::tile::TileMap(m_HGE);
			if(NULL != m_TileMap)
			{
				if(!m_TileMap->Load("asset/desert.tmx"))
				{
					return false;
				}

				x = y = 0.0f;
			}
			else
			{
				return false;
			}

			// gui
			m_Gui = new projectcos::gui::GuiManager(m_HGE);
			m_Gui->SetFont("../asset/guifont.fnt");

			hgeGUIText* temp = new hgeGUIText(1, 20, 20, 200, 20, m_Gui->GetFont());
			temp->SetText("Test GUI!");
			temp->SetColor(0xFF000000);
			m_Gui->AddCtrl(temp);

			return true;
		}
	}
	else
	{
		return false;
	}

	return true;
}

bool GameEngine::Start()
{
	if(NULL != m_HGE)
	{
		// it will run until frameFunc return true
		return m_HGE->System_Start();
	}

	return false;
}

void GameEngine::Shutdown()
{
	if(NULL != m_TileMap)
	{
		delete m_TileMap;
		m_TileMap = NULL;
	}

	if(NULL != m_HGE)
	{
//		m_HGE->Texture_Free(quad.tex);

		m_HGE->System_Shutdown();
		m_HGE->Release();

		m_HGE = NULL;
	}
}

bool GameEngine::Update(float dt)
{
	if (m_HGE->Input_GetKeyState(HGEK_ESCAPE))
	{
		return GAME_EXIT;
	}

	if(m_HGE->Input_GetKeyState(HGEK_LEFT))
	{
		x += 300 * dt;
	}
	else if(m_HGE->Input_GetKeyState(HGEK_RIGHT))
	{
		x -= 300 * dt;
	}

	if(m_HGE->Input_GetKeyState(HGEK_UP))
	{
		y += 300 * dt;
	}
	else if(m_HGE->Input_GetKeyState(HGEK_DOWN))
	{
		y -= 300 * dt;
	}

	return GAME_CONT;
}

bool GameEngine::Render()
{
	if(NULL != m_HGE)
	{

		m_HGE->Gfx_BeginScene();
		m_HGE->Gfx_Clear(WINDOW_BACKGROUND);

		//------------------------- Render stuff here -----------------------------------------------------------------------------------

		if(NULL != m_TileMap)
		{
			m_TileMap->Render(x, y);
		}

		if(NULL != m_Gui)
		{
			m_Gui->Render();
		}

		//-------------------------------------------------------------------------------------------------------------------------------

		m_HGE->Gfx_EndScene();
	}

	return GAME_CONT;
}

} // core
} // projectcos
